<?php

namespace EasySwoole\EasySwoole;


use App\Exceptions\ExceptionHandler;
use App\Queue\SmsSend\SmsSendProcess;
use App\Queue\SmsSend\SmsSendQueue;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\HotReload\HotReload;
use EasySwoole\HotReload\HotReloadOptions;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\Db\Config;
use EasySwoole\FastCache\Cache;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\RedisPool\Redis;
use EasySwoole\RedisPool\RedisPool;
use EasySwoole\Queue\Driver\Redis as RedisDriver;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        $instance = \EasySwoole\EasySwoole\Config::getInstance();
        $globalConfig = $instance->getConf('DATABASE');
        ### 启用Mysql数据库连接池
        $dbConfig = new Config();
        $dbConfig->setDatabase($globalConfig['database']);
        $dbConfig->setUser($globalConfig['user']);
        $dbConfig->setPassword($globalConfig['pass']);
        $dbConfig->setHost($globalConfig['host']);

        $dbConfig->setGetObjectTimeout(3.0);
        $dbConfig->setIntervalCheckTime(30 * 1000);
        $dbConfig->setMaxIdleTime(15);
        $dbConfig->setMaxObjectNum(20);
        $dbConfig->setMinObjectNum(5);
        $dbConfig->setAutoPing(5);

        DbManager::getInstance()->addConnection(new Connection($dbConfig));
        ### 数据库链接配置结束 
        ### Redis数据库配置
        $redisConf = $instance->getConf('REDIS');
        $rConfig = new RedisConfig();
        $rConfig->setHost($redisConf['host']);
        $rConfig->setPort($redisConf['port']);
        $rConfig->setAuth($redisConf['auth']);
        $rConfig->setDb($redisConf['db']);
        $rConfig->setSerialize($redisConf['serialize']);

        $redisPoolConfig = Redis::getInstance()->register('redis', $rConfig);
        $redisPoolConfig->setMinObjectNum(5);
        $redisPoolConfig->setMaxObjectNum(20);
        $redisPoolConfig->setAutoPing(10);
        ########################################



        Di::getInstance()->set(SysConst::HTTP_EXCEPTION_HANDLER, [ExceptionHandler::class, 'handle']);

        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        ########  热更新 ##########
        $hotReloadOptions = new HotReloadOptions;
        $hotReload = new HotReload($hotReloadOptions);
        $hotReloadOptions->setMonitorFolder([EASYSWOOLE_ROOT . '/App']);
        $server = ServerManager::getInstance()->getSwooleServer();
        $hotReload->attachToServer($server);
        ########  热更新 ##########

        ############### Sms_Send_Process ########
//        $config = new RedisConfig([
//            'host'=>'127.0.0.1'
//        ]);
//        $redis = new RedisPool($config);
//        $driver = new RedisDriver($redis,'SMS_QUEUE');
//        SmsSendQueue::getInstance($driver);
//        //注册一个消费进程
//        \EasySwoole\Component\Process\Manager::getInstance()->addProcess(new SmsSendProcess());
        ##################END ####
        Cache::getInstance()->setTempDir(EASYSWOOLE_TEMP_DIR)->attachToServer(ServerManager::getInstance()->getSwooleServer());


    }

    public static function onRequest(Request $request, Response $response): bool
    {

        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}